package com.gyg.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gyg.domain.CloudUser;
import com.gyg.dao.CloudUserMapper;
import com.gyg.returns.RespCode;
import com.gyg.returns.RespEntity;
import com.gyg.returns.RespTable;
import com.gyg.service.CloudUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gyg.vo.MapVO;
import com.gyg.vo.param.LoginParam;
import com.gyg.vo.param.PageParam;
import com.gyg.vo.param.SearchUserParam;
import com.gyg.vo.result.SexResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 用户信息表 服务实现类
 * </p>
 *
 * @author gyg
 * @since 2021-02-20
 */
@Service
@Slf4j
public class CloudUserServiceImpl extends ServiceImpl<CloudUserMapper, CloudUser> implements CloudUserService {

    @Override
    public RespEntity<CloudUser> login(LoginParam loginParam) {
        if (Objects.isNull(loginParam)) {
            log.info("参数非法或者格式不正确！");
            return new RespEntity(RespCode.PARAMETERS_ERROR);
        }

        QueryWrapper<CloudUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(CloudUser::getUsername, loginParam.getUsername())
                .eq(CloudUser::getPassword, loginParam.getPassword());

        CloudUser cloudUser = baseMapper.selectOne(queryWrapper);

        return new RespEntity(RespCode.SUCCESS, cloudUser);
    }

    @Override
    public RespEntity<CloudUser> userInfo(Integer userId) {
        CloudUser cloudUser = baseMapper.selectById(userId);
        return new RespEntity<>(RespCode.SUCCESS, cloudUser);
    }

    @Override
    public RespTable<List<CloudUser>> cloudUserList(Integer page, Integer limit, String column, String value) {
        QueryWrapper<CloudUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(CloudUser::getDeleted, 0);

        //模糊搜索
        if (StringUtils.isNotBlank(column) && StringUtils.isNotBlank(value)) {
            queryWrapper.like(column, value);
        }

        //分页实现
        IPage<CloudUser> iPage = new Page<>(page, limit);
        IPage<CloudUser> pg = super.page(iPage, queryWrapper);

        return new RespTable<>(RespCode.OK, pg.getRecords(), pg.getTotal());
    }

    @Override
    public RespEntity cloudUserBan(Integer userId) {
        CloudUser cloudUser = CloudUser.builder()
                .id(userId)
                .deleted(1)
                .build();
        baseMapper.updateById(cloudUser);

        return new RespEntity(RespCode.SUCCESS);
    }

    @Override
    public RespEntity cloudUserDeBlock(Integer userId) {
        CloudUser cloudUser = CloudUser.builder()
                .id(userId)
                .deleted(0)
                .build();
        baseMapper.updateById(cloudUser);

        return new RespEntity(RespCode.SUCCESS);
    }

    @Override
    public RespTable<List<CloudUser>> cloudUserBanList(PageParam pageParam) {
        QueryWrapper<CloudUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(CloudUser::getDeleted, 1);

        IPage<CloudUser> iPage = new Page<>(pageParam.getPage(), pageParam.getLimit());
        IPage<CloudUser> page = super.page(iPage, queryWrapper);

        return new RespTable<>(RespCode.OK, page.getRecords(), page.getTotal());
    }

    @Override
    public RespEntity<List<CloudUser>> queryUserByIds(Integer[] ids) {
        List<CloudUser> cloudUserList = baseMapper.selectBatchIds(Arrays.asList(ids));
        return new RespEntity<>(RespCode.SUCCESS, cloudUserList);
    }

    @Override
    public RespEntity<List<CloudUser>> queryAllUser() {
        QueryWrapper<CloudUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(CloudUser::getDeleted, 0)
                .eq(CloudUser::getBan, 0);
        List<CloudUser> cloudUserList = baseMapper.selectList(queryWrapper);

        return new RespEntity<>(RespCode.SUCCESS, cloudUserList);
    }

    @Override
    public RespEntity<SexResult> querySexCount() {
        QueryWrapper<CloudUser> queryWrapperMan = new QueryWrapper<>();
        queryWrapperMan.lambda().eq(CloudUser::getDeleted, 0)
                .eq(CloudUser::getBan, 0)
                .eq(CloudUser::getSex, 1);
        Integer manCount = baseMapper.selectCount(queryWrapperMan);

        QueryWrapper<CloudUser> queryWrapperWomen = new QueryWrapper<>();
        queryWrapperWomen.lambda().eq(CloudUser::getDeleted, 0)
                .eq(CloudUser::getBan, 0)
                .eq(CloudUser::getSex, 2);
        Integer womenCount = baseMapper.selectCount(queryWrapperWomen);

        return new RespEntity<>(RespCode.SUCCESS, new SexResult(manCount, womenCount));
    }

    @Override
    public RespEntity<List<MapVO>> chinaMap(String beginDate, String endDate) {
        List<MapVO> mapVOList = baseMapper.chinaMap(beginDate, endDate);
        for (MapVO mapVO : mapVOList) {

            if (StringUtils.isBlank(mapVO.getName())) {
                continue;
            }

            if (mapVO.getName().contains("市")) {
                String name = mapVO.getName().split("市")[0];
                mapVO.setName(name);
                continue;
            }
            if (mapVO.getName().contains("省")) {
                String name = mapVO.getName().split("省")[0];
                mapVO.setName(name);
            }
        }
        return new RespEntity<>(RespCode.SUCCESS, mapVOList);
    }

    @Override
    public RespEntity<List<MapVO>> provinceMap(String province, String beginDate, String endDate) {
        switch (province) {
            case "北京" : province = "北京市"; break;
            case "天津" : province = "天津市"; break;
            case "重庆" : province = "重庆市"; break;
            case "上海" : province = "上海市"; break;
            default: province = province + "省";
        }
        List<MapVO> mapVOList = baseMapper.provinceMap(province, beginDate, endDate);
        return new RespEntity<>(RespCode.SUCCESS, mapVOList);
    }

}
